import type { Restaurant, DrawHistory, RestaurantMark } from '../api/types'

// 时间判断工具函数
export class TimeUtils {
  // 获取今天结束时间
  static getEndOfToday(): string {
    const now = new Date()
    now.setHours(23, 59, 59, 999)
    return now.toISOString()
  }
  
  // 获取本周结束时间
  static getEndOfWeek(): string {
    const now = new Date()
    const endOfWeek = new Date(now.setDate(now.getDate() - now.getDay() + 6))
    endOfWeek.setHours(23, 59, 59, 999)
    return endOfWeek.toISOString()
  }
  
  // 判断是否为本月
  static isThisMonth(date: string): boolean {
    const targetDate = new Date(date)
    const now = new Date()
    return targetDate.getFullYear() === now.getFullYear() && 
           targetDate.getMonth() === now.getMonth()
  }
  
  // 判断是否为本周
  static isThisWeek(date: string): boolean {
    const targetDate = new Date(date)
    const now = new Date()
    const startOfWeek = new Date(now.setDate(now.getDate() - now.getDay()))
    const endOfWeek = new Date(now.setDate(now.getDate() - now.getDay() + 6))
    return targetDate >= startOfWeek && targetDate <= endOfWeek
  }
}

// 抽签引擎
export class DrawEngine {
  
  // 判断本月是否已抽过
  private isDrawnThisMonth(restaurantId: string, history: DrawHistory[]): boolean {
    const thisMonth = new Date()
    thisMonth.setDate(1)
    thisMonth.setHours(0, 0, 0, 0)
    
    return history.some(record => 
      record.restaurant_id === restaurantId && 
      new Date(record.drawn_at) >= thisMonth
    )
  }
  
  // 判断本周是否已抽过
  private isDrawnThisWeek(restaurantId: string, history: DrawHistory[]): boolean {
    const now = new Date()
    const startOfWeek = new Date(now.setDate(now.getDate() - now.getDay()))
    startOfWeek.setHours(0, 0, 0, 0)
    
    return history.some(record => 
      record.restaurant_id === restaurantId && 
      new Date(record.drawn_at) >= startOfWeek
    )
  }
  
  // 判断是否在最近3次中
  private isInRecentThree(restaurantId: string, history: DrawHistory[]): boolean {
    const recentThree = history.slice(0, 3)
    return recentThree.some(record => record.restaurant_id === restaurantId)
  }
  
  // 判断今天是否被标记
  private isMarkedToday(restaurantId: string, marks: RestaurantMark[]): boolean {
    return marks.some(mark => 
      mark.restaurant_id === restaurantId && 
      mark.mark_type === 'today' && 
      new Date(mark.expires_at) > new Date()
    )
  }
  
  // 判断本周是否被标记
  private isMarkedThisWeek(restaurantId: string, marks: RestaurantMark[]): boolean {
    return marks.some(mark => 
      mark.restaurant_id === restaurantId && 
      mark.mark_type === 'week' && 
      new Date(mark.expires_at) > new Date()
    )
  }
  
  // 获取符合条件的餐厅（纯逻辑方法，需要外部传入数据）
  getEligibleRestaurants(allRestaurants: Restaurant[], recentHistory: DrawHistory[], activeMarks: RestaurantMark[]): Restaurant[] {
    return allRestaurants.filter(restaurant => {
      // 排除本月已抽过的餐厅
      if (this.isDrawnThisMonth(restaurant.id, recentHistory)) {
        return false
      }
      
      // 排除本周已抽过的餐厅
      if (this.isDrawnThisWeek(restaurant.id, recentHistory)) {
        return false
      }
      
      // 排除最近3次抽过的餐厅
      if (this.isInRecentThree(restaurant.id, recentHistory)) {
        return false
      }
      
      // 排除今天标记的餐厅
      if (this.isMarkedToday(restaurant.id, activeMarks)) {
        return false
      }
      
      // 排除本周标记的餐厅
      if (this.isMarkedThisWeek(restaurant.id, activeMarks)) {
        return false
      }
      
      return true
    })
  }
  
  // 执行随机抽签（纯逻辑方法）
  drawRestaurant(eligibleRestaurants: Restaurant[]): Restaurant | null {
    if (eligibleRestaurants.length === 0) {
      return null // 没有符合条件的餐厅
    }
    
    const randomIndex = Math.floor(Math.random() * eligibleRestaurants.length)
    return eligibleRestaurants[randomIndex]
  }
}